﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snake
{
    class GameLogic
    {
        private GameEngine ge;        
        private Field field;
        private Snake.SnakeItem snakeItem;
        private Snake snake;
        private Food food;

        private GameRender renderGame;
        private GameRender renderField;
        private GameRender renderFood;
        private GameRender renderSnake;

        private GameController gc;

        public GameLogic()
        {
            ge = new GameEngine();
            ge.Tick += OnTickFired;
        }

        void OnTickFired(object sender, EventArgs e)
        {
            renderGame.ClearConsole();
            renderField.RenderField(field);           
            renderFood.RenderFoodOnConsole(food);
            this.snake.Move(snake.ChangeSnakeDirection(GetDirectionFromConsole(gc.GetPressedKey(gc.GetConsoleKey()))));            
            renderSnake.RenderSnake(snake);
        }      

        public void StartGame()
        {
            this.field = new Field();
            this.snakeItem = new Snake.SnakeItem();  
            this.snake = new Snake(snakeItem.GetStartXCoordinate(), snakeItem.GetStartYCoordinate());                      
            this.food = new Food(10, 10);
            this.renderField = new GameRender();
            this.renderFood = new GameRender();
            this.renderSnake = new GameRender();
            this.renderGame = new GameRender();
            this.gc = new GameController();
 
            ge.Start();
        }

        public string GetDirectionFromConsole(string direction)
        {
           
            string newDirection = snake.ChangeSnakeDirection(direction);
            return newDirection;
            
        }


        
    }
}
